package com.board.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.board.dao.AdminInfoMapper;
import com.board.dao.ServiceInfoMapper;
import com.board.entity.AdminInfo;
import com.board.entity.ServiceInfo;
import com.board.exception.ServiceInfoException;
import com.board.param.AddServiceParam;
import com.board.service.ServiceInfoService;
import com.board.util.PageInfo;
import com.board.util.StateDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 维修工程的服务指南表 服务实现类
 * </p>
 *
 * @author 小叶
 * @since 2024年04月03日
 */
@Service
public class ServiceInfoServiceImpl extends ServiceImpl<ServiceInfoMapper, ServiceInfo> implements ServiceInfoService {
    @Autowired
    private AdminInfoMapper adminInfoMapper;
    @Autowired
    private ServiceInfoMapper serviceInfoMapper;

    @Override
    public ServiceInfo addServiceInfo(AddServiceParam param, Integer adminId) {
        //查询管理员信息
        AdminInfo adminInfo = adminInfoMapper.selectById(adminId);
        //新增服务指南
        ServiceInfo serviceInfo = BeanUtil.toBean(param, ServiceInfo.class);
        serviceInfo.setServiceAuthor(adminInfo.getAdminUser());
        serviceInfo.setServiceInfoTime(LocalDateTime.now().toString());
        //新增的服务指南默认是隐藏
        serviceInfo.setServiceInfoState(StateDate.SERVICE_STATE_DOWN);
        serviceInfoMapper.insert(serviceInfo);
        return serviceInfo;
    }

    @Override
    public ServiceInfo showService(Integer serviceInfoId) {
        //查询服务指南是否存在
        ServiceInfo serviceInfo = serviceInfoMapper.selectById(serviceInfoId);
        if (serviceInfo==null) throw new ServiceInfoException(500,"服务指南不存在");
        //已删除和已上架的不能操作
        if (serviceInfo.getServiceInfoState()==StateDate.SERVICE_STATE_DELETE){
            throw new ServiceInfoException(500,"服务指南已删除");
        }
        if (serviceInfo.getServiceInfoState()==StateDate.SERVICE_STATE_SHOW){
            throw new ServiceInfoException(500,"服务指南不能重复显示");
        }
        //修改服务指南状态
        serviceInfo.setServiceInfoState(StateDate.SERVICE_STATE_SHOW);
        serviceInfoMapper.updateById(serviceInfo);
        return serviceInfo;
    }

    @Override
    public ServiceInfo downServiceInfo(Integer serviceInfoId) {
        //查询服务指南是否存在
        ServiceInfo serviceInfo = serviceInfoMapper.selectById(serviceInfoId);
        if (serviceInfo==null) throw new ServiceInfoException(500,"服务指南不存在");
        //是否被删除
        if (serviceInfo.getServiceInfoState()==StateDate.SERVICE_STATE_DELETE){
            throw new ServiceInfoException(500,"服务指南已删除");
        }
        //是否已隐藏
        if (serviceInfo.getServiceInfoState()==StateDate.SERVICE_STATE_DOWN){
            throw new ServiceInfoException(500,"不能重复隐藏");
        }
        //修改状态
        serviceInfo.setServiceInfoState(StateDate.SERVICE_STATE_DOWN);
        serviceInfoMapper.updateById(serviceInfo);
        return serviceInfo;
    }

    @Override
    public void deleteServiceInfo(Integer serviceInfoId) {
        //服务指南是否存在
        ServiceInfo serviceInfo = serviceInfoMapper.selectById(serviceInfoId);
        if (serviceInfo==null) throw new ServiceInfoException(500,"服务指南不存在");
        //不能重复删除
        if (serviceInfo.getServiceInfoState()==StateDate.SERVICE_STATE_DELETE){
            throw new ServiceInfoException(500,"不能重复删除");
        }
        //修改状态
        serviceInfo.setServiceInfoState(StateDate.SERVICE_STATE_DELETE);
        serviceInfoMapper.updateById(serviceInfo);
    }

    @Override
    public ServiceInfo findServiceById(Integer serviceInfoId) {
        ServiceInfo serviceInfo = serviceInfoMapper.selectById(serviceInfoId);
        if (serviceInfo==null) throw new ServiceInfoException(500,"服务指南不存在");
        return serviceInfo;
    }

    @Override
    public PageInfo<ServiceInfo> userFindService(Integer pageNum, Integer pageSize) {
        Page<ServiceInfo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        QueryWrapper<ServiceInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("service_info_id");
        wrapper.eq("service_info_state",StateDate.SERVICE_STATE_SHOW);
        serviceInfoMapper.selectPage(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(page.getRecords());
        return pageInfo;
    }

    @Override
    public PageInfo<ServiceInfo> adminFindService(String serviceTitle, Integer pageNum, Integer pageSize) {
        Page<ServiceInfo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        QueryWrapper<ServiceInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("service_info_id");
        wrapper.ne("service_info_state",StateDate.SERVICE_STATE_DELETE);
        if (serviceTitle!=null&&serviceTitle!=""){
            wrapper.like("service_title",serviceTitle);
        }
        serviceInfoMapper.selectPage(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(page.getRecords());
        return pageInfo;
    }
}
